import 'package:dio/dio.dart';
import '../../res/string_res.dart';
import '../../services/SPUtils.dart';
import '../../services/utils.dart';


class ResponseHandlerInterceptor implements Interceptor {
  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    return handler.next(err);
  }

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    return handler.next(options);
  }
  
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) async {
    try {
      // final data = jsonDecode(response.toString());
      // int e = (data['e'] as int);
      // if (e == 10042) {
      //   UserUtils.showLogoutDialog();
      //   return;
      // }

      myLog("拦截器 onResponse ${response.data.toString()}");
      final data = response.data;
      int? e = int.tryParse (data['e'].toString());
      myLog("拦截器 onResponse的e $e");
      if (e == 10013) {
        SPUtils.saveData(SPKeys.loginExpired, true);
        // UserUtils.showLogoutDialog();
        return;
      }

    } catch (e) {
      myLog("Error al procesar respuesta: $e");
    }
    return handler.next(response);
  }
}
